<?php

namespace App\Console\Commands;

use App\Models\City;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class RedisInitCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'redis:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'redis初始化';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        City::where('parent_id', '=', 0)->chunkById(100, function ($items) {
            foreach ($items as $item) {
                /**
                 * @var City $item
                 */
                $item = Redis::geoadd('city', $item->lng, $item->lat, $item->id);
            }
        });
    }
}
